<template>
  <section class="textContent">
    <section class="messageText" :class="isSelf ? 'isSelf' : null">
      <span v-html="emojiTest?.content" class="text" v-if="row?.msgType === 'TEXT'">
      </span>
      <img :src="row?.content" alt="" v-if="row?.msgType === 'IMAGE'" @click="openFile">
    </section>
    <section v-if="emojiTest?.msgText" class="msg_quote">
      <i v-html="emojiTest?.msgName + ':' + emojiTest?.msgText"></i>
    </section>
  </section>
</template>
<script setup>
import { computed, defineProps } from 'vue';
import { useStore } from 'vuex';
import { db } from '@/config/data'
const props = defineProps({
  row: {
    type: Object,
    default: () => ({})
  }
})
const store = useStore()
const isSelf = computed(() => store?.state?.userInfo?.userId === props?.row?.userId)
const passText = (str) => {
  str = String(str)
  Array.from(new Array(59)).forEach((v, r) => {
    const t = `[f0${r < 10 ? ('0' + r) : r}]`
    if (str?.includes(t)) {
      str = str.replaceAll(t, `<img src="local-media://emoji/emoji_${r}@3x.png" class='emoji' style='width:20px;height:20px;'/>`)
    }
  })
  const hasNotice = str.includes('群公告\n')
  if (hasNotice) str = str.replaceAll('群公告\n', '群公告<br/>')
  if (str.startsWith('http')) str = `<img src="${str}" class='quote_img' />` //一张图片
  return str
}
const emojiTest = computed(() => {
  let msg = ''
  try {
    const result = JSON.parse(props?.row?.content)
    result?.content ? msg = result : (msg = props?.row?.content)
  } catch (error) {
    msg = props?.row?.content
  }
  if (typeof msg === 'object' && msg.content && msg.msgText) {
    //引用消息
    return {
      content: passText(msg.content),
      msgId: msg.msgId,
      msgName: msg.msgName,
      msgText: passText(msg.msgText)
    }
  }
  return {
    content: passText(String(msg))
  }
})
const openFile = async () => {
  const form = props.row.msgCustomType === 1 ? 'groupChat' : 'privateChat'
  const msg = await Reflect.get(db, form).where({ msgType: 'IMAGE', uuid: props.row.uuid }).toArray()
  const imgs = msg?.map(r => r.content)
  const index = imgs.findIndex(r => r === props.row.content)
  const native = await window.electronAPI.openNativeWindow("/preview")
  window.electronAPI.sendMsg({ id: native.id, imgs, index, url: props.row.content })
  native.focus()
}

</script>

<style scoped lang="less">
.textContent {
  position: relative;
  display: flex;
  flex-direction: column;
}

.messageText {
  width: auto;
  max-width: 300px;
  background-color: rgba(221, 223, 223, 1);
  text-align: left;
  padding: 10px;
  background: #DDDFDF;
  box-shadow: inset 0px 0px 4px 0px #DFDFDF;
  border-radius: 10px;
  border-top-left-radius: 0;
  display: inline-block;
  align-self: var(--showAlign);
  line-height: 18px;

  &.isSelf {
    border-top-right-radius: 0;
    border-top-left-radius: 10px;
    background: linear-gradient(115deg, #009BFD 8%, #00E8E8 100%);
    right: 0;
    top: 0;

    .text {
      color: #fff;
    }
  }

  .text {
    overflow: hidden; //文本超出隐藏
    text-overflow: ellipsis; //文本超出省略号替代
    text-align: left;
    word-wrap: break-word;
    word-break: normal;
    letter-spacing: 1px;
    color: #242424;
    line-height: 22px;

    :deep(.emoji) {
      vertical-align: bottom;
      display: inline-block;
    }
  }

  >img {
    max-width: 200px;
  }

  .emoji {
    width: 25px;
    height: 25px;
  }


}

.msg_quote {
  font-size: 14px;
  max-width: 300px;
  left: 0;
  padding: 8px 12px;
  text-align: left;
  font-weight: 400;
  color: #737373;
  text-align-last: left;
  background: #E7E7E7;
  border-radius: 4px;
  margin-top: 5px;
  letter-spacing: 1px;
  display: flex;
  align-items: flex-start;

  >i {
    word-wrap: break-word;
    word-break: break-all;
    max-width: 100%;
    white-space: pre-wrap;
    text-align: left;
    text-align: justify;
    line-height: 20px;
    display: inline-flex;
    align-items: center;
    text-align-last: left;
  }

  :deep(.quote_img) {
    // width: 40px;
    height: 40px;
  }
}
</style>
